/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.qms.dao.impl;

import com.opgea.qms.dao.TokenDetailsDAO;
import com.opgea.qms.domain.entities.TokenDetails;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import java.util.Locale;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Ramesh
 */
@Repository
@Transactional
public class TokenDetailsDAOImpl implements TokenDetailsDAO{

    //private Session session = HibernateUtil.getSessionFactory().openSession();
    @Autowired
    private SessionFactory sessionFactory;
    

    @Override
    public TokenDetails create(TokenDetails tokenDetails){
        sessionFactory.openSession().save(tokenDetails);
        return tokenDetails;
    }

    @Override
    public TokenDetails update(TokenDetails tokenDetails) {
        Session session = sessionFactory.openSession();
        Transaction tx = session.getTransaction();
        tx.begin();
        TokenDetails tDetails = (TokenDetails)session.get(TokenDetails.class, tokenDetails.getId());
        tDetails.setFirstName(tokenDetails.getFirstName());
        tDetails.setMiddleInitial(tokenDetails.getMiddleInitial());
        tDetails.setLastName(tokenDetails.getLastName());
        tDetails.setMobileNo(tokenDetails.getMobileNo());
        tDetails.setDestination(tokenDetails.getDestination());
        tDetails.setTokenStatus(tokenDetails.getTokenStatus());
        session.update(tDetails);
        tx.commit();
        sessionFactory.openSession().update(tokenDetails);
        return tDetails;
    }

    @Override
    public TokenDetails delete(Long tokenDetailsId) {
        TokenDetails tokenDetails = this.find(tokenDetailsId);
        sessionFactory.openSession().delete(tokenDetails);
        return tokenDetails;
    }

    @Override
    public TokenDetails find(Long id) {
        Query query = sessionFactory.openSession().getNamedQuery("TokenDetails.findById")
                .setParameter("id", id);
        TokenDetails tokenDetails = (TokenDetails) query.uniqueResult();
        return tokenDetails;
    }

    @Override
    public List<TokenDetails> findAll() {
        Query query = sessionFactory.openSession().getNamedQuery("TokenDetails.findAll");
        //List<TokenDetails> tokens = sessionFactory.openSession().createQuery("from TokenDetails").list();
        List<TokenDetails> tokens = query.list();
        return tokens;
    }

    @Override
    public List<TokenDetails> findAllByCompanyId(Long companyId) {
        Query query = sessionFactory.openSession().getNamedQuery("TokenDetails.findAllByCompanyId")
                .setParameter("companyId", companyId);
        List<TokenDetails> tokens = query.list();
        return tokens;
    }

    @Override
    public List<TokenDetails> findAllByDestination(Long destinationId) {
        Query query = sessionFactory.openSession().getNamedQuery("TokenDetails.findAllByDestination")
                .setParameter("destinationId", destinationId);
        List<TokenDetails> tokens = query.list();
        return tokens;
    }

    @Override
    public List<TokenDetails> findAllByDestination(Long companyId, Long destinationId) {
        /*
        Query query = sessionFactory.openSession().getNamedQuery("TokenDetails.findAllByDestination")
                 .setParameter("companyId", companyId)
                .setParameter("destinationId", destinationId);
         * 
         */
        Query query = sessionFactory.openSession().createQuery("SELECT t FROM TokenDetails t WHERE t.token.company.id = '"+companyId+"' and t.destination.id = '"+destinationId+"'");
        System.out.println("Query String: "+query.getQueryString());
        List<TokenDetails> tokens = query.list();
        return tokens;
    }

    @Override
    public List<TokenDetails> searchTokens(Long companyId, String searchKey, String tokenDate) {
        Date today = Calendar.getInstance().getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = sdf.format(today);
        Session session = sessionFactory.openSession();
        StringBuilder query = new StringBuilder();
        query.append(" SELECT t FROM TokenDetails t WHERE t.token.company.id = ");
        query.append(companyId);
        if(tokenDate != null && !tokenDate.equalsIgnoreCase("")){
            query.append(" AND t.token.tokenDate = ");
            query.append("'");
            query.append(tokenDate);
            query.append("'");
        }else{
            query.append(" AND t.token.tokenDate = ");
            query.append("'");
            query.append(dateString);
            query.append("'");
        }
        if(searchKey != null){
            query.append(" AND t.metaData like ");
            query.append("'%");
            query.append(searchKey);
            query.append("%'");
        }
        System.out.println("Search Query:"+query.toString());
        Query queryObject = session.createQuery(query.toString());
        List<TokenDetails> tokenDetails =  queryObject.list();
        return tokenDetails;
    }
    
}
